﻿'   fTray modMain 
'   Copyright (c) 2010 Marvin Westmaas ( MtM / Marvin_The_Martian )
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program.  If not, see <http://www.gnu.org/licenses/>.
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Module modMAIN
    ' fTray is ment for the smp console clients, and offer a system tray icon and graphical configuration option instead of 
    ' the console window.
    ' 
    ' Can and will be extended upon. The google code page for this project should have an archived copy of maxFah from which
    ' features will be ported and completed, among other things.
    ' 
    ' Current status is working but with a few 'hacks' like ->
    '   
    ' Dynamic updates should be working, timed loop only taking few cycles for clock/eta/icon update ( iconhover ??!! )
    ' Config form has no silent config function ( have another copy which has, need to update )
    ' Bonus factor points calculation is off, ppd calculation might be off completely
    ' There is no options form, application will show on execution both on desktop and systray
    ' About box closing closes main form
    ' Project info is only updated on project start and has no update function yet ( needs to be ported from maxFAH )
    ' Extended project browser/editor needs porting
    ' clsEOC clsStatistics from maxFAH needs porting 
    ' Thing need fixing when that's done -> EUE tracking, frame tracking ( add processing time for fahCore process and
    ' percentage utilisation, usefull in statistics ). 
    '
    ' When that's all done -> make fTray dockable in parent application.
    ' 
    ' Parent application when installed should do ->
    '   1. central projectinfo download ( no need for multiple locations then )
    '   2. able to control each client from single interface
    '   3. talk with a web back end to send statistics to ( frame statistics, EUE statistics )
    '   4. be able to uninstall/add clients on hw change/user preference
    '   5. use the web back end to gather settings template's for diffrent clients/os's/hw combinations

#Region "Entry point"
    Sub main()
        Try
            My.Settings.Reload()
            nIcon = fLog.nIcon
            ' Application.Run(frmLOG)
            If ClientPath = "-1" Or ClientPath = "-2" Then
                MsgBox("No client found, application will now exit", vbOKOnly)
                Application.Exit()
            Else
                Application.EnableVisualStyles()
                Application.Run(fLog)
            End If
        Catch ex As Exception

        Finally
            'If ClientControl.IsService = False And ClientControl.ConsoleStatus = clsClientControl.eStatus.active And ClientControl.WindowState = clsClientControl.eWindowState.Hidden Then
            'ClientControl.ShowConsole()
            'End If
        End Try
    End Sub
#End Region
#Region "Dll imports"
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function GetConsoleWindow() As IntPtr
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function AttachConsole(ByVal procid As Integer) As Boolean
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function FreeConsole() As Boolean
    End Function
    <DllImport("user32.dll")> _
    Public Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("user32.dll")> _
    Public Function GetForegroundWindow() As Long
    End Function
    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function GenerateConsoleCtrlEvent(ByVal ctrlEvent As Integer, ByVal procid As Integer) As Boolean
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
    End Function
#End Region
#Region "Properties"
#Region ".net version installed"
    Public Enum eDotNetVersion
        Err = 0
        Two = 1
        Three = 2
        ThreePointFive = 3
    End Enum
    Public ReadOnly Property InstalledDOTNETVersion() As eDotNetVersion
        Get
            Try
                Dim rKey As Microsoft.Win32.RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP")
                Dim sName() As String = rKey.GetSubKeyNames
                Dim rDot As eDotNetVersion = eDotNetVersion.Err
                For Each InstVersion As String In sName
                    InstVersion = InstVersion.Replace("v", "")
                    If Mid(InstVersion, 1, 3) = "2.0" And rDot < eDotNetVersion.Two Then
                        rDot = eDotNetVersion.Two
                    ElseIf Mid(InstVersion, 1, 3) = "3.0" And rDot < eDotNetVersion.Three Then
                        rDot = eDotNetVersion.Three
                    ElseIf Mid(InstVersion, 1, 3) = "3.5" And rDot < eDotNetVersion.ThreePointFive Then
                        rDot = eDotNetVersion.ThreePointFive
                    End If
                Next
                Return rDot
            Catch ex As Exception
                Return eDotNetVersion.Err
            End Try
        End Get
    End Property
#End Region
    Public ReadOnly Property ClientPath As String
        Get
            Try
                Dim tPath As String = Application.StartupPath
                If My.Computer.FileSystem.FileExists(tPath & "\Folding@home-Win32-x86.exe") Then
                    Return tPath
                ElseIf My.Computer.FileSystem.FileExists(tPath & "\Folding@home-Win32-GPU.exe") Then
                    Return tPath
                Else
                    Return "-1"
                End If
            Catch ex As Exception
                Return "-2"
            End Try
        End Get
    End Property
    Public ReadOnly Property ClientEXE As String
        Get
            Try
                If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Folding@home-Win32-x86.exe") Then
                    Return Application.StartupPath & "\Folding@home-Win32-x86.exe"
                ElseIf My.Computer.FileSystem.FileExists(Application.StartupPath & "\Folding@home-Win32-GPU.exe") Then
                    Return Application.StartupPath & "\Folding@home-Win32-GPU.exe"
                End If
            Catch ex As Exception

            End Try
        End Get
    End Property
    Public ReadOnly Property ClientProcessName As String
        Get
            Dim tString As String = ClientEXE.Replace(".exe", "")
            Return Mid(tString, tString.LastIndexOf("\") + 2)
        End Get
    End Property
#End Region
#Region "Constants"
    Public Const SW_HIDE As Integer = 0
    Public Const SW_SHOWNORMAL As Integer = 1
    Public Const SW_SHOWMINIMIZED As Integer = 2
    Public Const SW_SHOWMAXIMIZED As Integer = 3
    Public Const SW_SHOWNOACTIVATE As Integer = 4
    Public Const SW_RESTORE As Integer = 9
    Public Const SW_SHOWDEFAULT As Integer = 10
    Public Const CTRL_C_EVENT As Integer = 0
#End Region
#Region "Declares"
    Public WithEvents ClientControl As clsClientControl
    Public fLog As New frmLOG
    Public LogWindow As New clsLogwindow
    Public ProjectInfo As clsProjectInfo
    Public MySettings As New clsSettings
    Public nIcon As NotifyIcon
#End Region
#Region "Functions and subs"
    Public Sub WaitMS(ByVal MS As Long)
        Try
            Dim dEnd As DateTime = DateTime.Now.AddMilliseconds(MS)
            Do While dEnd.Subtract(DateTime.Now).TotalMilliseconds > 0
                Application.DoEvents()
            Loop
        Catch ex As Exception

        End Try
    End Sub
#End Region
End Module
